import { assign } from "fair-wing"

export function useShortcuts(item: any): any[] {
  const defaultShortcuts = [
    {
        text: $t('job.log.today'),
        value: () => {
          // 今日 返回的时间是 [今日 00:00:00, 今日 23:59:59]
          const end = new Date()
          const start = new Date()
          if(item.config?.type?.includes('time')) {
            start.setHours(0, 0, 0)
            end.setHours(23, 59, 59)
          }
          if(item.config?.type?.includes('range')) return [start, end]
          return start
        },
      },
      {
        text: $t('job.log.yesterday'),
        value: () => {
          // 昨日 返回的时间是 [昨日 00:00:00, 昨日 23:59:59]
          const start = new Date()
          const end = new Date()
          start.setTime(start.getTime() - 3600 * 1000 * 24)
          end.setDate(start.getDate()); // 周日
          end.setHours(23, 59, 59)
          if(item.config?.type?.includes('range')) return [start, end]
          return start
        },
      },
      {
        text: $t('job.log.thisWeek'),
        value: () => {
          const today = new Date();
          const start = new Date(today);
          start.setDate(today.getDate() - today.getDay() + (today.getDay() === 0 ? -6 : 1)); // 周一
          const end = new Date(start);
          end.setDate(start.getDate() + 6); // 周日
          if(item.config?.type?.includes('time')) {
            start.setHours(0, 0, 0);
            end.setHours(23, 59, 59);
          }
          if(item.config?.type?.includes('range')) return [start, end]
          return start;
        },
      },
      {
        text: $t('job.log.lastWeek'),
        value: () => {
          const today = new Date();
          const start = new Date(today);
          start.setDate(today.getDate() - today.getDay() - 6); // 上周一
          const end = new Date(start);
          end.setDate(start.getDate() + 6); // 上周日
          if(item.config?.type?.includes('time')) {
            start.setHours(0, 0, 0);
            end.setHours(23, 59, 59);
          }
          if(item.config?.type?.includes('range')) return [start, end]
          return start;
        },
      },
      {
        text: $t('job.log.last7Days'),
        value: () => {
          const end = new Date()
          const start = new Date()
          start.setDate(start.getDate() - 7)
          if(item.config?.type?.includes('range')) return [start, end]
          return start
        },
      },
      {
        text: $t('job.log.lastMonth'),
        value: () => {
          const end = new Date()
          const start = new Date()
          start.setMonth(start.getMonth() - 1)
          if(item.config?.type?.includes('range')) return [start, end]
          return start
        },
      },
      ...(item.config?.type?.includes('time') ? [{
        text: $t('job.log.lastHour'),
        value: () => {
          const end = new Date()
          const start = new Date()
          start.setHours(start.getHours() - 1)
          if(item.config?.type?.includes('range')) return [start, end]
          return start
        },
      }] : []),
  ]

  return assign([], defaultShortcuts, item.config?.shortcuts || []) as any
}